<template>
    <div class="container">
        <el-header class="header">
            <el-button class="back" @click="goBack">
                <el-icon>
                    <ArrowLeft />
                </el-icon>
            </el-button>
            <span class="title">消息</span>
        </el-header>

        <el-main class="main-content">
            <div v-if="loading" class="loading">加载中...</div>
            <div v-else-if="currentMessage" class="notice-detail">
                <h1 class="notice-title">
                    {{ currentMessage.adminId ? '职位调动通知' : '审批通知' }}
                </h1>
                <div class="notice-content-box">
                    <pre class="notice-content">{{ currentMessage.content }}</pre>
                </div>
                <div class="notice-footer">
                    <div class="notice-meta">
                        <div class="notice-dept">发送人：{{ senderName }}</div>
                        <div class="notice-time">{{ currentMessage.sendTime }}</div>
                    </div>
                    <el-button type="primary" class="confirm-btn" @click="confirmRead(currentMessage.id)"
                        :disabled="currentMessage.isRead === 1" style="width: 70px;">
                        {{ currentMessage.isRead === 1 ? '已确认' : '确认已读' }}
                    </el-button>
                </div>
            </div>
            <div v-else class="no-data">消息不存在</div>
        </el-main>
    </div>
</template>

<script>
import { getMessageDetail, markAsRead } from "../../api/message.js";
import { getAdminNameById } from "../../api/admin.js";
import { getSessionStorageObj } from "../../utils/common.js";
import { getEmpNameById } from "../../api/basedata.js";

//得到全局数据
let userInfo = getSessionStorageObj('userInfo');
console.log('用户信息:', userInfo);

export default {
    data() {
        return {
            empId: userInfo.empId,
            loading: false,
            currentMessage: null,
            senderName: "",
        };
    },
    methods: {
        goBack() {
            this.$router.push("/message");
        },

        // api-获取发送人姓名
        async fetchSenderName() {
            if (!this.currentMessage) return;
            
            try {
                let res;
                if (this.currentMessage.adminId) {
                    // 如果是管理员发送的消息
                    res = await getAdminNameById({ id: this.currentMessage.adminId });
                } else if (this.currentMessage.leaderId) {
                    // 如果是领导发送的消息
                    res = await getEmpNameById({ id: this.currentMessage.leaderId });
                }
                
                this.senderName = res?.flag ? res.result : "未知发送人";
            } catch (error) {
                console.error("获取发送人信息失败:", error);
                this.senderName = "未知发送人";
            }
        },

        // api-确认已读
        async confirmRead(messageId) {
            try {
                const res = await markAsRead({ id: messageId });
                if (res.flag && this.currentMessage) {
                    this.currentMessage.isRead = 1;
                }
            } catch (error) {
                console.error("标记已读失败:", error);
            }
        },

        // api-获取消息详情
        async fetchMessage() {
            this.loading = true;
            try {
                const res = await getMessageDetail({ id: this.$route.params.id });

                if (res.flag) {
                    this.currentMessage = { ...res.result };
                    await this.fetchSenderName();
                } else {
                    console.warn('消息不匹配或不存在');
                }
            } catch (error) {
                console.error("获取消息详情失败:", error);
                this.currentMessage = null;
            } finally {
                this.loading = false;
            }
        },
    },
    mounted() {
        this.fetchMessage();
    }
}
</script>

<style scoped>
.container {
    height: 100vh;
    width: 100vw;
    background-color: #e1efff;
    overflow-x: hidden;
    position: relative;
}

.header {
    height: 8vh;
    width: 100%;
    background-color: white;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
}

.main-content {
    padding-top: 8vh;
    overflow-y: auto;
}

.back {
    position: absolute;
    left: 20px;
    top: 50%;
    transform: translateY(-50%);
    border: none;
    font-size: 24px;
    background: transparent;
}

.notice-detail {
    background-color: white;
    margin: 20px;
    border-radius: 12px;
    padding: 24px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
}

.notice-title {
    color: #1a1a1a;
    font-size: 22px;
    font-weight: bold;
    text-align: center;
    margin-bottom: 24px;
    padding-bottom: 16px;
    border-bottom: 1px solid #f0f0f0;
}

.notice-content-box {
    padding: 0 10px;
    margin-bottom: 24px;
}

.notice-content {
    font-size: 16px;
    line-height: 1.8;
    color: #333;
    white-space: pre-wrap;
    word-wrap: break-word;
    font-family: "PingFang SC", "Microsoft YaHei", sans-serif;
    margin: 0;
}

.notice-footer {
    padding-top: 16px;
    border-top: 1px solid #f0f0f0;
}

.notice-meta {
    display: flex;
    flex-direction: column;
    gap: 8px;
    color: #666;
    font-size: 14px;
}

.notice-dept {
    color: #409eff;
    font-weight: 500;
}

.notice-time {
    color: #999;
}

.loading,
.no-data {
    text-align: center;
    padding: 50px;
    color: #909399;
    font-size: 16px;
}

/* 保留原有所有样式，新增以下样式 */
.confirm-btn {
    margin-top: 16px;
    float: right;
}

.notice-footer {
    display: flex;
    justify-content: space-between;
    align-items: flex-end;
    padding-top: 16px;
    border-top: 1px solid #f0f0f0;
}

.notice-meta {
    flex-grow: 1;
}
</style>